//
//  ViewController.swift
//  C020_test_customTableViewCell
//
//  Created by yao_yu on 15/6/30.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

func sum<S: SequenceType, T : IntegerType where S.Generator.Element == T >(s: S) -> T {
    var sum : T = 0
    for i in s {
        sum += i
    }
    return sum
}





class ViewController: UIViewController, YYSpreadSheetDataSource, YYSpreadSheetDelegate {
    
    var headers = [[String:String]]()
    var datas = [[String:String]]()
    
    
    var mTable:YYSpreadSheetTableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        mTable = YYSpreadSheetTableView(frame: CGRectZero)
        mTable.tableWidth = 1400

        generateDatas()
        mTable.fixedColumns = 3
        mTable.dataSource = self
        mTable.delegate = self
        
        view.addSubview(mTable)
        
        
    }
    
    func spreadSheet(spreadSheet: YYSpreadSheetTableView, didSelectRowAtIndex index: Int) {
        print(datas[index])
    }
    
    func generateDatas() {
        
        var mCellDefines = [(name:String, width:CGFloat)]()
        for i in 0..<20 {
            let a:(name:String, width:CGFloat) = ("cell\(i)", CGFloat( 1 ))
            mCellDefines.append(a)
        }
        mTable.columnDefines = mCellDefines

        for i in 0..<2 {
            var row = [String:String]()
            for (j, cellDefine) in mCellDefines.enumerate() {
                row[cellDefine.name] = "表头(\(i),\(j))"
            }
            headers.append(row)
        }

        var myTableData = [[String:String]]()
        for i in 0..<100 {
            var row = [String:String]()
            for (j, cellDefine) in mCellDefines.enumerate() {
                row[cellDefine.name] = "单元格(\(i),\(j))"
            }
            myTableData.append(row)
        }
        datas = myTableData
    }
    
    override func viewDidLayoutSubviews() {        
        mTable.frame = CGRectMake(0, 20, view.frame.width, view.frame.height - 20)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var mTable:UITableView!
    var scrollView:UIScrollView!
    var myTableData: [[String:String]]!
    var mCellDefines: [(name:String, width:CGFloat)]!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        mTable = UITableView(frame: CGRectZero)
        mTable.separatorStyle = UITableViewCellSeparatorStyle.None
        mTable.delegate = self
        mTable.dataSource = self
        scrollView = UIScrollView(frame: CGRectMake(0, 20, view.frame.width, view.frame.height - 20))
        mTable.frame = CGRectMake(0, 20, view.frame.width, view.frame.height - 20)
        view.addSubview(scrollView)
        scrollView.addSubview(mTable)
        scrollView.contentSize = CGSize(width: mTable.frame.width, height: mTable.frame.height)
        generateDatas()
        
    }
    
    func generateDatas() {
        
        mCellDefines = [(name:String, width:CGFloat)]()
        for i in 0..<20 {
            let a:(name:String, width:CGFloat) = ("cell\(i)", CGFloat( 1 ))
            mCellDefines.append(a)
        }
        
        myTableData = [[String:String]]()
        for i in 0..<10000 {
            var row = [String:String]()
            for (j, cellDefine) in mCellDefines.enumerate() {
                row[cellDefine.name] = "cell(\(i),\(j))"
            }
            myTableData.append(row)
        }
    }
    
    override func viewDidLayoutSubviews() {
        scrollView.frame = CGRectMake(0, 20, view.frame.width, view.frame.height - 20)
        mTable.frame = CGRectMake(0, 20, view.frame.width * 4, view.frame.height - 20)
        scrollView.contentSize = CGSize(width: mTable.frame.width, height: mTable.frame.height)
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myTableData == nil ? 0 : myTableData.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var myCell:YYSpreadSheetTableViewCell?
        myCell = tableView.dequeueReusableCellWithIdentifier("myCell") as? YYSpreadSheetTableViewCell
        if myCell == nil {
            myCell = YYSpreadSheetTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell")
        }
        let row = myTableData[indexPath.row]
//        for j in 0..<10 {
//            row.append("cell(\(indexPath.row),\(j))")
//        }
        //myCell?.totalWidth = mTable.frame.width
        //myCell?.columns = row.count// myTableData[indexPath.row].count
        myCell?.cellDefines = mCellDefines
        myCell?.rowData = row // myTableData[indexPath.row]
        return myCell!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

